//picker
var baseUrl = "/index.php/index/Connectthethird/";
var isProLoaded = false;
var isCityLoaded = false;
var isDealerLoaded = false;
var proData, cityData, dealerData, selProCode, selCityCode, selDealerCode,selProName,selCityName,selDealerName;
$.ajax({ //获取省
    url: baseUrl + "getProvince",
    success(res) {
        console.log(res);
        proData = res.data;
        if (!proData) {
            alert("数据加载失败！");
            return;
        }
        isProLoaded = true;

        if (isProLoaded && isCityLoaded && isDealerLoaded) {
            onAreaInfoOk()
        }
    }
});
$.ajax({ //获取市
    url: baseUrl + "getCity",
    success(res) {
        console.log(res);
        cityData = res.data;
        if (!cityData) {
            alert("数据加载失败！");
            return;
        }
        isCityLoaded = true;

        if (isProLoaded && isCityLoaded && isDealerLoaded) {
            onAreaInfoOk()
        }
    }
});
$.ajax({ //获取经销商
    url: baseUrl + "getDealers",
    success(res) {
        console.log(res);
        dealerData = res.data;
        if (!dealerData) {
            alert("数据加载失败！");
            return;
        }
        isDealerLoaded = true;

        if (isProLoaded && isCityLoaded && isDealerLoaded) {
            onAreaInfoOk()
        }
    }
});
//数据获取完成后回调
function onAreaInfoOk() {
    var proData_picker = [{
        text: "请选择",
        value: 0
    }];
    var cityData_picker = [{
        text: "请选择",
        value: 0
    }];
    var dealerData_picker = [{
        text: "请选择",
        value: 0
    }];

    var proPicker = new Picker({
        data: [proData_picker],
        selectedIndex: [0],
    });
    var cityPicker = new Picker({
        data: [cityData_picker],
        selectedIndex: [0],
    });
    var dealerPicker = new Picker({
        data: [dealerData_picker],
        selectedIndex: [0],
    });

    proData_picker = proData.map(function (cur) {
        cur.text = cur.province_name;
        cur.value = cur.province_code;
        return cur;
    })
    proPicker = new Picker({
        data: [proData_picker],
        selectedIndex: [0],
    });
    proPicker.on('picker.select', function (selectedVal, selectedIndex) {
        $(".province span").text(proData_picker[selectedIndex].text);
        selProCode = proData_picker[selectedIndex].value;
        selProName = proData_picker[selectedIndex].text;
        cityData_picker = [{
            text: "请选择",
            value: 0
        }];
        cityData.forEach(function (cur) {
            if (cur.city_code.indexOf(selProCode) != -1) {
                cur.text = cur.city_name;
                cur.value = cur.city_code;
                cityData_picker.push(cur);
            }
        })
        // cityPicker.refill([[{text:"请选择",value:0}]]);//重新刷新数据,方法报错
        cityPicker = new Picker({
            data: [cityData_picker],
            selectedIndex: [0],
        });
        addCityMethod();
    });

    function addCityMethod() {
        //初始化城市，经销商
        selCityCode = 0;
        $(".city span").text("选择城市");
        selDealerCode = 0;
        $(".dealer span").text("选择经销商");

        cityPicker.on('picker.select', function (selectedVal, selectedIndex) {
            $(".city span").text(cityData_picker[selectedIndex].text);
            selCityCode = cityData_picker[selectedIndex].value; //选中的城市code
            selCityName = cityData_picker[selectedIndex].text;
            dealerData_picker = [{
                text: "请选择",
                value: 0
            }];
            dealerData.forEach(function (cur) {
                if (cur.dealer_city_code == selCityCode) {
                    cur.text = cur.dealer_name;
                    cur.value = cur.dealer_code;
                    dealerData_picker.push(cur);
                }
            })
            dealerPicker = new Picker({
                data: [dealerData_picker],
                selectedIndex: [0],
            });
            addDealerMethod();
        });
    }

    function addDealerMethod() {
        selDealerCode = 0;
        $(".dealer span").text("选择经销商");
        dealerPicker.on('picker.select', function (selectedVal, selectedIndex) {
            $(".dealer span").text(dealerData_picker[selectedIndex].text);
            selDealerCode = dealerData_picker[selectedIndex].value; //选中的经销商code
            selDealerName = dealerData_picker[selectedIndex].text;
            console.log("结果", selProCode, selCityCode, selDealerCode);
        });
    }



    $(".province").on("click", function () {
        proPicker.show();
    })
    $(".city").on("click", function () {
        cityPicker.show();
    })
    $(".dealer").on("click", function () {
        dealerPicker.show();
    })
}

//提交按钮
var myNotice;
$(".submitBtn").on("click", function() {
    MtaH5.clickStat("liuzi");//统计数据用
	console.log("省", selProCode,selProName);
	console.log("市", selCityCode,selCityName);
	console.log("经销商", selDealerCode,selDealerName);
	var customer_name = $("#userName").val();
	if(!customer_name) {
		$(document).dialog({
			overlayShow:true,
			titleShow: false,
			overlayClose: true,
			content: '用户名不能为空！',
		});
		return;
	}
	var mobile = $("#mobile").val();
	if(!(/^1[3-8]\d{9}$/.test(mobile))) {
		$(document).dialog({
			overlayShow:true,
			titleShow: false,
			overlayClose: true,
			content: '请输入正确的手机号码！',
		});
		return;
	}
	if(!selProCode || !selCityCode || !selDealerCode) {
		$(document).dialog({
			titleShow: false,
			overlayClose: true,
			content: '请选择相关信息！',
		});
		return;
	}
	myNotice = $(document).dialog({
		overlayShow:true,
		type: 'notice',
		infoIcon: 'img/icon/loading.gif',
		infoText: '提交中',
	});
	$.ajax({
		url: baseUrl + "setSalesLeads",
		data: {
			"province_code": selProCode,
			"province_name": selProName,
			"city_code": selCityCode,
			"city_name": selCityName,
			"dealer_code": selDealerCode,
			"dealer_name": selDealerName,
			// "modle_code": selModelCode,
			// "modle_name": selModelName,
			"mobile": mobile,
			"customer_name": customer_name,
			"gender": "0"
		},
		success(res) {
            console.log(res);
            setTimeout(function(){
                $(".shareWeb").css("display","block");
            },1000)
            if(res.res == 1) {//数据插入成功
				myNotice.update({
					overlayShow:true,
					infoIcon: 'img/icon/success.png',
					infoText: '提交成功！',
					autoClose: 2000
                });
			} else if(res.res == 2){//数据更新成功
				myNotice.update({
					overlayShow:true,
					infoIcon: 'img/icon/success.png',
					infoText: '更新成功！',
					autoClose: 2000
                });
               
			}else{
				myNotice.update({
					overlayShow:true,
					infoIcon: 'img/icon/fail.png',
					infoText: '提交失败！',
					autoClose: 2000
				});
			}
		},
		fail() {
			myNotice.update({
				overlayShow:true,
				infoIcon: 'img/icon/fail.png',
				infoText: '提交失败！',
				autoClose: 2000
			});
		}
	})
})